package com.guigu.youxuan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.GoodsInfoVo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.common.vo.GoodsVo;
import com.guigu.youxuan.pojo.GoodsCategory;
import com.guigu.youxuan.pojo.GoodsInfo;
import com.guigu.youxuan.pojo.dto.GoodsDto;
import com.guigu.youxuan.service.IGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/goodsInfo")
public class GoodsInfoController {
    /**
     * 导入商品接口
     */
    @Autowired
    private IGoodsInfoService iGoodsInfoService;


    @RequestMapping("/updateStatus")
    @ResponseBody
    public String updateStatus(@RequestBody GoodsInfo goodsInfo) {
        iGoodsInfoService.updateStatus(goodsInfo);
        if (goodsInfo.getStatus() == 0) {
            return "下架成功";
        }
        return "上架成功";
    }
    /**
     * 供应商带条件分页查询
     * @param vo
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/SupplierListPage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<GoodsInfo> SupplierListPage(@RequestBody GoodsInfoVo vo,
                                                @PathVariable int currentPage,
                                                @PathVariable int pageSize){
        PageInfo<GoodsInfo> page=this.iGoodsInfoService.SupplierListPage(vo,currentPage,pageSize);
        return page;
    }
    /**
     * 供应商根据商品表id查询对应的商品信息
     * @param id
     * @return
     */
    @RequestMapping("/goodsInfoById/{id}")
    @ResponseBody
    public GoodsInfo goodsInfoById(@PathVariable int id){
        GoodsInfo goodsInfo=this.iGoodsInfoService.getById(id);
        return goodsInfo;
    }

    /**
     * 模糊查询商品信息
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/selGoods/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<GoodsDto> selgoods(@PathVariable String keyword,@PathVariable int pageNo, @PathVariable int pageSize){
        PageUtils<GoodsDto> goodsInfos = iGoodsInfoService.selGoods(keyword,pageNo, pageSize);
       return goodsInfos;
    }

    /**
     * ID查询商品详情页信息
     * @param id
     * @return
     */
    @RequestMapping("/selGoodsById/{id}")
    @ResponseBody
    public List<GoodsInfo> selgoods(@PathVariable int id){
        List<GoodsInfo> goodsById = iGoodsInfoService.selGoodsById(id);
        return goodsById;
    }

    /**
     * 查询水果热销
     * @return
     */
    @RequestMapping("/selFruits")
    @ResponseBody
    public List<GoodsDto> selFruits(){
        List<GoodsDto> goodsDtos = iGoodsInfoService.selFruits();
        return goodsDtos;
    }

    /**
     * 查询酒水热销
     * @return
     */
    @RequestMapping("/selDrinks")
    @ResponseBody
    public List<GoodsDto> selDrinks(){
        List<GoodsDto> goodsDtos = iGoodsInfoService.selDrinks();
        return goodsDtos;
    }

    /**
     * 商品添加上架库存
     * @param amount
     * @param goodsId
     * @return
     */
    @RequestMapping("/addStockUp/{amount}/{goodsId}")
    @ResponseBody
    public String addStockUp(@PathVariable Integer amount,
                             @PathVariable Long goodsId) {
        iGoodsInfoService.addStockUp(amount, goodsId);
        return "添加上架库存成功！";
    }

    /**
     * 批量删除商品
     * @param goodsInfoList
     * @return
     */
    @RequestMapping("/delsGoods")
    @ResponseBody
    public String delsGoods(@RequestBody List<GoodsInfo> goodsInfoList) {
        iGoodsInfoService.delsGoods(goodsInfoList);
        return "删除成功！";
    }

    @RequestMapping("/delGoods")
    @ResponseBody
    /**
     * 删除商品
     * @param goodsInfo
     * @return
     */
    public String delGoods(@RequestBody GoodsInfo goodsInfo) {
        iGoodsInfoService.delGoods(goodsInfo);
        return "删除成功！";
    }

    /**
     * 修改商品
     * @param vo
     * @return
     */
    @RequestMapping("/updateGoods")
    @ResponseBody
    public String updateGoods(@RequestBody GoodsVo vo) {
        iGoodsInfoService.updateGoods(vo);
        return "修改商品成功！";
    }

    /**
     * 修改商品的回显查询
     * @param id
     * @return
     */
    @RequestMapping("/queryGoodsById/{id}")
    @ResponseBody
    public GoodsInfo queryGoodsById(@PathVariable Long id) {
        GoodsInfo goodsInfo = iGoodsInfoService.queryGoodsById(id);
        return goodsInfo;
    }

    /**
     * 添加商品
     * @param vo
     * @return
     */
    @RequestMapping("/addGoods")
    @ResponseBody
    public String addGoods(@RequestBody GoodsVo vo) {
        iGoodsInfoService.addGoods(vo);
        return "新增商品成功！";
    }

    /**
     * 展示图上传
     * @return
     */
    @RequestMapping("/avatarImgUpload")
    @ResponseBody
    public String avatarImgUpload(MultipartFile file) {


        //获取源文件名(包含后缀 1.jpg)
        String originalFilename = file.getOriginalFilename();
        //截取后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成唯一文件名
        String newFileName = UUID.randomUUID().toString()+suffix;
        //该文件要放在指定目录下
        File dir = new File("D:/myimgs/Goods");
        if(!dir.exists())
            dir.mkdir();
        //目标文件对象
        File destFile = new File(dir,newFileName);

        //文件写入
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:10001/images/Goods/"+newFileName;
    }

    /**
     * 商品
     * @param goodsInfo
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/getGoodsList/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<GoodsDto> getGoodsList(@RequestBody GoodsInfo goodsInfo,
                                            @PathVariable int currentPage,
                                            @PathVariable int pageSize) {
        PageUtils<GoodsDto> page = iGoodsInfoService.getGoodsList(goodsInfo,currentPage,pageSize);
        return page;
    }
}
